package com.zshield.entry;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.zshield.util.Etime;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

public class LinuxNetioEntry implements EntryInterface<LinuxNetioEntry> {
    @SerializedName("RCV_BYTES")
    private long rcvBytes;
    @SerializedName("XMT_BYTES")
    private long xmtBytes;
    @SerializedName("OPERATE_SIZE")
    private long operateSize;
    @SerializedName("TIME")
    private String time;
    @SerializedName("DAY_TIME")
    private String dayTime;
    @SerializedName("@timestamp")
    private String timeStamp;
    @SerializedName("FORMAT")
    private String format;
    @SerializedName("SENSOR_ID")
    private String sensorId;
    @SerializedName("PTYPE")
    private String ptype;
    @SerializedName("EXE_NAME")
    private String exeName;
    @SerializedName("EXE_PATH")
    private String exePath;
    @SerializedName("FILE_HASH")
    private String fileHash;
    @SerializedName("BIND_PORT")
    private Set<Integer> bindPort;
    @SerializedName("COMMAND_LINE")
    private String commandLine;
    @SerializedName("COMPUTER")
    private String computer;

    public LinuxNetioEntry(JsonObject obj) {
        rcvBytes = obj.get("RCV_BYTES").getAsLong();
        xmtBytes = obj.get("XMT_BYTES").getAsLong();
        operateSize = rcvBytes + xmtBytes;
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        format = "PRE_COMPUTE";
        sensorId = obj.get("SENSOR_ID").getAsString();
        ptype = "LnxApp";
        exeName = obj.get("EXE_NAME").getAsString();
        exePath = obj.get("EXE_PATH").getAsString();
        fileHash = obj.get("FILE_HASH").getAsString();
        commandLine = obj.get("COMMAND_LINE").getAsString();
        computer = obj.get("COMPUTER").getAsString();
        bindPort = new HashSet<>();
        if (obj.get("IS_BIND_PORT").getAsInt() == 1) {
            bindPort.add(obj.get("DST_PORT").getAsInt());
        }
    }

    @Override
    public void update(LinuxNetioEntry that) {
        this.rcvBytes = this.rcvBytes + that.rcvBytes;
        this.xmtBytes = this.xmtBytes + that.xmtBytes;
        this.operateSize = this.operateSize + that.operateSize;
        this.time = that.time;
        this.timeStamp = that.timeStamp;
        this.bindPort.addAll(that.bindPort);
    }

    @Override
    public String getDayTime() {
        return this.dayTime;
    }

    @Override
    public String getDocId() {
        return "linuxNetIo~" + sensorId + "~" + exeName + "~" + commandLine + "~" + fileHash + "~" + dayTime;
    }
}
